/*
 * Funny Language - a free style programming language.
 * Copyright (C) 2015 by fanguangping (fanguangping@163.com)
 * test_hash.c
 */

#include <string.h>
#include "runner_ds.h"

static void test_alloc_hash_table() {
	HashTable table = alloc_hash_table();
	assert_non_null(table);
}

static void test_alloc_hash_node() {
	HashNode *node = alloc_hash_node();
	assert_non_null(node);
}

static void test_init_table() {
	HashTable table = init_table();
	assert_non_null(table);
	assert_null(table[0]);
	assert_null(table[HASHTABLE_SIZE - 1]);
}

static void test_put_table_element() {
	HashTable table = init_table();
	put_table_element(NULL, NULL, NULL, NULL);
	put_table_element(table, "abc", "123", hash_code_string);
	put_table_element(table, "def", "456", hash_code_string);
    assert_string_equal(table[hash_code_string("abc")]->element, "123");
    assert_string_equal(table[hash_code_string("def")]->element, "456");
}

static void test_get_table_element() {
	HashTable table = init_table();
	put_table_element(table, "abc", "123", hash_code_string);
	put_table_element(table, "def", "456", hash_code_string);
	put_table_element(table, "def", "789", hash_code_string);
    assert_string_equal(get_table_element(table, "abc", hash_code_string)->element, "123");
    assert_string_equal(get_table_element(table, "def", hash_code_string)->element, "456");
    assert_string_equal(get_table_element(table, "def", hash_code_string)->next->element, "789");
	assert_null(get_table_element(NULL, NULL, NULL));
}

static void test_clear_table() {
	clear_table(NULL);
	HashTable table = init_table();
	put_table_element(table, "abc", "123", hash_code_string);
	clear_table(table);
	assert_null(get_table_element(table, "abc", hash_code_string));
}

int main(int argc, char **argv) {
	const struct CMUnitTest tests[] = {
			cmocka_unit_test(test_alloc_hash_table),
			cmocka_unit_test(test_alloc_hash_node),
			cmocka_unit_test(test_init_table),
			cmocka_unit_test(test_put_table_element),
			cmocka_unit_test(test_get_table_element),
			cmocka_unit_test(test_clear_table),
	};
	return cmocka_run_group_tests(tests, setup_ds, teardown_ds);
}
